<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var dataTablesDoctorOrder = $('#dataTablesDoctorOrder').DataTable({
    order:[]
    ,columns: [
        {"bSortable": false,"width":80, "render":function (data, type, full, meta) {
                return '<input type="checkbox" class="sel-do-list" value="'+ full.id +'" checked data-price="'+ full.sum_payable +'">';
            }},
        {"mDataProp" : "doctororder.status", "bSortable": false, "render":function (data, type, full, meta) {
                var xu = '';
                var pay = '';
                switch (parseInt(full.doctororder.pay_status)) {
                    case 0:pay ='未支付';break;
                    case 1:pay ='已支付';break;
                    case 2:pay ='待退费';break;
                    case 3:pay ='已退费';break;
                }
                switch (parseInt(data)) {
                    case 0:xu ='<span>待发送</span> ';break;
                    case 1:xu ='<span class="am-text-secondary">已发送('+ pay +')</span> ';break;
                    case 2:xu ='<span class="am-text-warning">已回退('+ pay +')</span> ';break;
                    case 3:xu ='<span class="am-text-danger">已退费('+ pay +')</span>';break;
                    case 4:xu ='<span style="color: gray">已作废('+ pay +')</span>';break;
                }
                return '<span class="number" style="white-space:nowrap;" data-index="'+ meta.row +'" data-number="'+ full.doctororder.number.id +'" data-prescdate="'+ timetransDate(full.doctororder.number.created_at) +'">'+xu+'</span><input type="hidden" class="project_type" value="'+ full.doctororder.project_type +'"><input type="hidden" class="total_price" value="'+ full.doctororder.total_price +'">';
            }},
        {"mDataProp" : "doctororder.content", "bSortable": false, "render":function (data, type, full, meta) {
                return '<span class="content">'+ data +'</span>';
            }},
        {"mDataProp" : "doctororder.dosage_zl", "bSortable": false,"render":function (data, type, full, meta) {
                return '<span class="dosage_zl">'+ data +'</span>';
            }},
        {"mDataProp" : "doctororder.unit", "bSortable": false,"render":function (data, type, full, meta) {
                return '<span class="unit">'+ data +'</span>';
            }},
        {"mDataProp" : "doctororder.unit_price", "bSortable": false,"render":function (data, type, full, meta) {
                return '<span class="unit_price">'+ data +'</span>';
            }},
        {"mDataProp" : "sum_payable", "bSortable": false,"render":function (data, type, full, meta) {
                return '<span class="total_price">'+ data +'</span>';
            }},
        {"mDataProp" : "created_at", "bSortable": false,"render":function (data, type, full, meta) {
                return '<span class="created_at">'+ timetrans(data) +'</span>';
            }}
    ]
    ,'bLengthChange': false
    ,"serverSide": false
    ,"bPaginate" : false
    ,"bFilter" : false
    ,"bInfo" : false
    ,"bProcessing" : false
    ,"drawCallback": function ( settings ) {

    }
});
$('#dataTablesDoctorOrder tbody').on( 'click','tr',function(){
    var chk = $(this).find('input[type="checkbox"]');
    if(chk.prop('checked')) {
        chk.prop('checked', false);
        chk.parent().parent().addClass('gray');
    } else {
        chk.prop('checked', true);
        chk.parent().parent().removeClass('gray');
    }
    calculation_money();
});
$('.pay_type,#real_money').keyup(function(){
    calculation_money();
});
function input_init() {
    $('#real_money,#cut_money,.pay_type').val(0);
}

var sel_member;

$('#saveCollectfees').click(function () {
    if(!sel_member) {
        layer.alert('患者信息不存在');return false;
    }
    var cash_money = parseFloat(checkValueIsNaN('cash_money'));
    var total_money = parseFloat(checkValueIsNaN('total_money'));
    var sum_payable = parseFloat(checkValueIsNaN('sum_payable'));
    var wechat_money = parseFloat(checkValueIsNaN('wechat_money'));
    var card_money = parseFloat(checkValueIsNaN('card_money'));
    var alipay_money = parseFloat(checkValueIsNaN('alipay_money'));
    var archives_id = parseInt(sel_member.Id);
    var real_money = parseFloat(checkValueIsNaN('real_money'));
    if(!archives_id) {
        layer.alert('患者信息不存在');return false;
    }
    if(sum_payable<=0) {
        layer.alert('应付金额不正确');return false;
    }
    if(cash_money > 0 && real_money<= 0) {
        layer.alert('实收金额不足');return false;
    }
    if(cash_money<0) {
        layer.alert('实收金额不正确');return false;
    }
    if(cash_money > real_money) {
        layer.alert('实收金额不足');return false;
    }
    var doctor_ids = [];
    var sel_length = $('#dataTablesDoctorOrder tbody').find('.sel-do-list').length;
    $('#dataTablesDoctorOrder tbody').find('.sel-do-list').each(function (idx ,val) {
        if($(this).prop('checked')) {
            doctor_ids.push($(this).val());
        }
    });
    if(doctor_ids.length <= 0) {
        layer.alert('找不到可结帐信息');return false;
    }
    var save_data = {
        'logs_id':sel_detail_info.id,
        'cash_money':cash_money,
        'total_money':total_money,
        'sum_payable':sum_payable,
        'wechat_money':wechat_money,
        'card_money':card_money,
        'alipay_money':alipay_money,
        'archives_id':archives_id,
        'doctor_ids' : doctor_ids.join(',')
    };
    sel_length = sel_length - doctor_ids.length;
    if(confirm("确定执行退费吗？")) {
        $.post('{{  urlfor "CollectfeesController.LogsOut" }}',save_data, function (res) {
            if(res.code == 1) {
                layer.alert(res.msg); return false;
            }
            var tipsMsg = res.msg;
            if(sel_length > 0) {
                tipsMsg = '已选项目退费成功，未选项目自动转为待结状态，请及时结帐';
            }
            layer.alert(tipsMsg,function (index) {
               parent.layer.closeAll();
               parent.dialogReback();
            });
        }, 'json').error(function () {
            layer.alert('结算失败请重试');
        });
    }
    return false;
});

function checkValueIsNaN(tar) {
    return  $('#'+tar).val() ? $('#'+tar).val() : 0;
}
//查询处方列表
var sel_detail_info;
function doctorOrderQuery() {
    $.post('{{urlfor "CollectfeesController.LogsDetail"}}',{'id':getQueryVariable('id'),'pay_status':'1'}, function(res){
        if(res.code == 1) {
            return false;
        }
        var dt = $('#dataTablesDoctorOrder').DataTable();
        dt.clear().draw();
        $.each(res.data.doctor_order, function (index, value) {
            dt.row.add(value).draw();
        });
        sel_detail_info = res.data;

        if(sel_detail_info.archives) {
            var uinfo = sel_detail_info.archives ;
            var main_obj = $('.userinfo');
            main_obj.find('.name').html(uinfo.Realname);
            main_obj.find('.sex').html(uinfo.Sex);
            main_obj.find('.age').html(uinfo.Age+'岁');
            main_obj.find('.mobile').html(uinfo.Mobile);
            main_obj.find('.birthday').html(uinfo.Birthday);
            sel_member = sel_detail_info.archives;
        }
        calculation_money();
    }, 'json');
}
doctorOrderQuery();

//计算需要支付金额
function calculation_money() {
    var total_money = 0; //总金额
    var real_money = 0;//实收金额
    var cash_money = 0;//现金金额
    $('#dataTablesDoctorOrder tbody').find('.sel-do-list').each(function (idx ,val) {
        if($(this).prop('checked')) {
            total_money += parseFloat($(this).data('price'));
        }
    });
    $('.pay_type').each(function(){
        if($(this).val()) {
            real_money +=  parseFloat($(this).val());
        }
    });
    cash_money = (total_money - real_money).toFixed(2);
    $('#cash_money').val(cash_money);
    $('#total_money').val(total_money);  //合计
    $('#sum_payable').val(total_money);  //应付
    real_money = parseFloat($('#real_money').val());
    if(real_money > 0) {
        cut_money = (real_money - cash_money).toFixed(2);
        $('#cut_money').val(cut_money); //找零
    }else {
        $('#cut_money').val(0); //找零
    }
}

</script>
